//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  GameControlViewController.m
//  PokerHandUp
//
//  Created by psvn047 on 6/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#import "GameControlViewController.h"
#import "GameViewController.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@interface GameControlViewController ()

@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation GameControlViewController
@synthesize handlerCallback = _handlerCallback;
CGRect rect;
bool moveSquare;
bool allowCheck;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Confirm raise handler
-(IBAction)confirmRaiseHandler:(id)sender
{
    [scrollView setHidden:YES];
    [controlView setUserInteractionEnabled:YES];
    // Calculate 
    float percent = (140 - imgTrackView.frame.origin.y) / 100;
    //
    [self hide];
    [self.handlerCallback raiseHandler:percent];
}

// raise handler
-(IBAction)raiseHandler:(id)sender
{
    [self updateScroll];
    [scrollView setHidden:NO];
    [controlView setUserInteractionEnabled:NO];
}
// check handler
-(IBAction)checkHandler:(id)sender
{
    [self hide];
    [self.handlerCallback checkHandler:allowCheck];
}
// fold handler
-(IBAction)foldHandler:(id)sender
{
    [self hide];
    [self.handlerCallback foldHandler];
}
-(void) updateScroll
{
    CGRect fr = imgTrackView.frame;
    [imgTrackView setFrame:CGRectMake(fr.origin.x, 140, fr.size.width, fr.size.height)];
    lblMoney.text = [NSString stringWithFormat:@"$%d",[[[GameViewController share] myPlayer] Money]];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	//NSSet *allTouches = [event allTouches];
    
	UITouch *touch = [touches anyObject];
	//CGPoint touchBegin = [touch locationInView:self.view];

	
	if ([touch view] == imgTrackView) {
		moveSquare = YES;
	}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint touchMoved = [touch locationInView:scrollView];
	
	if (moveSquare) 
    {
        if (touchMoved.y>=40 && touchMoved.y <=140) {
            CGRect fr = imgTrackView.frame;
            [imgTrackView setFrame:CGRectMake(fr.origin.x, touchMoved.y, fr.size.width, fr.size.height)];
            float percent =  (140 - imgTrackView.frame.origin.y) / 100;
            long Money = [GameViewController share].myPlayer.Money;
            lblMoney.text = [NSString stringWithFormat:@"$%d", Money - (long) (percent * Money)];
        }
       
    }
	
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	moveSquare = NO;
	//UITouch *touch = [touches anyObject];
	//CGPoint touchEnded = [touch locationInView:self.view];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        rect  = CGRectMake(0, 423, 320, 37);
    }
    return self;
}
-(void) updateControlBar:(BOOL) cCheck
{
       allowCheck = cCheck;
    if (cCheck) {
     
        [btnCheck setTitle:@"CHECK" forState:UIControlStateNormal];
    }
    else {
         [btnCheck setTitle:@"CALL" forState:UIControlStateNormal];
    }
}
// show control bar
-(void) show
{
    [UIView animateWithDuration:0.5f
                     animations:^{
                         [controlView setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)];
                     }
                     completion:^(BOOL finished) {
                         //[self hide];
                     }
     ];

}
// hide control bar
-(void) hide
{

    [UIView animateWithDuration:0.2f
                     animations:^{
                         [controlView setFrame:CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, rect.size.width, rect.size.height)];
                     }
                     completion:^(BOOL finished) {
                         //[self show];
                     }
     ];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [scrollView setHidden:YES];
    [self hide];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.handlerCallback = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////